﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TR.AppService.Dto;
using TR.AppService.Dto.wh_Buy_Order_hDto;
using TR.Domain.Entity;
using TR.Domain.Entity.BsItemType;
using TR.Domain.Entity.JH;
using TR.Domain.Repository.Wh_Buy_Order_hRepository;

namespace TR.AppService.AppService.Wh_Buy_Order_h
{
    public class wh_Buy_Order_hAppService : Iwh_Buy_Order_hAppService
    {
        private readonly Iwh_Buy_Order_hRepository wh_Buy_Order_hRepository;
        private readonly IMapper mapper;

        public wh_Buy_Order_hAppService(Iwh_Buy_Order_hRepository wh_Buy_Order_hRepository, IMapper mapper)
        {
            this.wh_Buy_Order_hRepository = wh_Buy_Order_hRepository;
            this.mapper = mapper;
        }

        //分页条件查询
        public async Task<DtoFenYe<wh_Buy_Order_h>> FenYeSelect(int mys, int dqy, string name = "")
        {
            var listFenYe = await wh_Buy_Order_hRepository.FenYeSelect(mys, dqy, name);
            var ztsFenYe = await wh_Buy_Order_hRepository.FenYeZtsSelect();

            var res = new DtoFenYe<wh_Buy_Order_h>()
            {
                mys = mys,
                dqy = dqy,
                list = listFenYe,
                zts = ztsFenYe
            };
            return res;
        }

        //信息新增
        public async Task<bool> InsertOrder(WBOHInsertDto jhzb, List<WODInsertDto> ddmx)
        {
            wh_Buy_Order_h jhinsert = mapper.Map<wh_Buy_Order_h>(jhzb);
            var ddinsert = mapper.Map<List<wh_orderbuy_d>>(ddmx);
            return await wh_Buy_Order_hRepository.InsertOrder(jhinsert, ddinsert);
        }

        //进货明细表查询
        public Task<List<JHXQ>> JHXQTjSelect(string tj)
        {
            return wh_Buy_Order_hRepository.JHXQTjSelect(tj);
        }

        //进货订单编号查询
        public Task<wh_Buy_Order_h> OrderBhSelect(string tj)
        {
            return wh_Buy_Order_hRepository.OrderBhSelect(tj);
        }

        //信息修改
        public async Task<bool> UpdateOrder(WBOHUpdateDto jHZB, List<WODUpdateDto> ddmx)
        {
            wh_Buy_Order_h jhupdate = mapper.Map<wh_Buy_Order_h>(jHZB);
            var ddupdate = mapper.Map<List<wh_orderbuy_d>>(ddmx);
            return await wh_Buy_Order_hRepository.UpdateOrder(jhupdate, ddupdate);
        }

        //供应商下拉框查询
        public async Task<List<bs_Supply>> bs_SubblySelect()
        {
            return await wh_Buy_Order_hRepository.bs_SupplieSelect();
        }

        //根据供应商查询联系人和联系方式
        public async Task<bs_Supply> bs_SupplyTjSelect(string tj)
        {
            return await wh_Buy_Order_hRepository.bs_SupplyTjSelect(tj);
        }

        //商品信息
        public async Task<DtoFenYe<bs_Item>> BsItemSelect(int mys, int dqy, string? tj)
        {
            var FenYeList = await wh_Buy_Order_hRepository.BsItemSelect(mys, dqy, tj);
            var hpzs = await wh_Buy_Order_hRepository.ShuLiang();
            var result = new DtoFenYe<bs_Item>()
            {
                mys = mys,
                dqy = dqy,
                list = FenYeList,
                zts = hpzs,
            };
            return result;
        }
    }
}
